package com.ehe.common.core.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.ehe.common.core.enumeration.BooleanType;
import com.ehe.common.core.web.pojo.domain.SuperEntity;
import com.ehe.common.core.web.service.SuperService;

import javax.annotation.Nonnull;
import java.io.Serializable;
import java.util.List;
import java.util.Optional;

public abstract class SuperServiceImpl<T extends SuperEntity<T, PK>, PK extends Serializable> extends MybatisServiceImpl<T, PK> implements SuperService<T, PK> {

    @Override
    public int logicDeleteChangeById(@Nonnull PK id, @Nonnull BooleanType isDelete) {
        T t = getSuperMapper().selectById(id);
        t.setIsDelete(isDelete);
        return getSuperMapper().updateById(t);
    }

    @Override
    public int logicDeleteById(@Nonnull PK id) {
        return logicDeleteChangeById(id, BooleanType.TRUE);
    }

    @Override
    public int logicDeleteReductionById(@Nonnull PK id) {
        return logicDeleteChangeById(id, BooleanType.FALSE);
    }

    @Override
    public List<T> listByFieldEquals(SFunction<T, String> function, Object object) {
        LambdaQueryWrapper<T> queryWrapper = createQueryWrapper().lambda();
        queryWrapper.eq(function, object);
        return getSuperMapper().selectList(queryWrapper);
    }

    @Override
    public Optional<T> oneByFieldEquals(SFunction<T, String> function, Object object) {
        LambdaQueryWrapper<T> queryWrapper = createQueryWrapper().lambda();
        queryWrapper.eq(function, object);
        return Optional.ofNullable(getSuperMapper().selectOne(queryWrapper));
    }
}
